<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>实例事件</title>
  <script src="../assets/js/vue.js"></script>
</head>

<body>
  <h1>实例事件</h1>
  <hr>
  <div id="app">
    {{num}}
    <p>
      <button @click="add">add</button>
    </p>
  </div>
  <button onclick="reduce()">reduce</button>
  <button onclick="reduceOnce()">reduceOnce</button>
  <button onclick="off()">off</button>
  <script type="text/javascript">
    var app = new Vue({
      el: '#app',
      data: {
        num: 1
      },
      methods: {
        add: function () {
          this.num++;
        }
      }
    })

    app.$on("reduce", function () {
      console.log("执行了reduce方法");
      this.num--;
    })

    app.$once('reduceOnce', function () {
      console.log('只执行一次的方法');
      this.num--;

    });

    function reduce() {
      app.$emit('reduce')
    }
    function reduceOnce() {
      app.$emit('reduceOnce')
    }
    function off() {
      app.$off('reduce');
    }
  </script>
</body>

</html>